package com.matchmaking.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.matchmaking.system.config.EasyExcelConfig;
import com.matchmaking.system.config.PageBean;
import com.matchmaking.system.config.R;
import com.matchmaking.system.domain.SysPost;
import com.matchmaking.system.domain.SysRole;
import com.matchmaking.system.domain.SysUser;
import com.matchmaking.system.minio.MinioService;
import com.matchmaking.system.service.SysDeptService;
import com.matchmaking.system.service.SysRoleService;
import com.matchmaking.system.service.SysUserService;
import com.matchmaking.system.utils.ExcelUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: TODO
 * @Author 卟哭！
 * @Date 2024/03/21  11:51
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private MinioService minimalService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private HttpServletResponse response;


    /**
     * 查询用户列表
     *
     * @param user
     * @param pageBean
     * @return
     */
    @GetMapping("list")
    public R selectUsers(SysUser user, PageBean pageBean) {
        PageHelper.startPage(pageBean);
        List<SysUser> list = sysUserService.selectObjects(user);
        PageInfo<SysUser> userList = new PageInfo<>(list);
        userList.getList().forEach(item -> {
            List<SysRole> sysRoles = sysRoleService.selectObjectsByUserId(item.getUserId());
            item.setRoles(sysRoles);
        });
        return R.successed(userList);
    }

    /**
     * 获取岗位列表
     *
     * @return
     */
    @GetMapping("postList")
    public R selectPosts() {
        List<SysPost> sysPosts = sysUserService.selectPosts();
        return R.successed(sysPosts);
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("roleList")
    public R selectRoles() {
        List<SysRole> list = sysUserService.selectRoles();
        return R.successed(list);
    }

    /**
     * 获取部门树状列表
     *
     * @return
     */
    @GetMapping("deptListTree")
    public R selectDeptsTree() {
        List<Tree<String>> deptsTree = sysUserService.selectDeptsTree();
        return R.successed(deptsTree);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */

    @PostMapping("update")
    public R updateUser(@RequestBody SysUser user) {
        boolean b = sysUserService.updateObjest(user);
        return b ? R.successed("修改成功") : R.failed("修改失败");
    }

    /**
     * 修改用户状态
     *
     * @param user
     * @return
     */
    @PostMapping("updateStatus")
    public R updateStatus(@RequestBody SysUser user) {
        boolean b = sysUserService.updateStatus(user);
        return b ? R.successed("状态修改成功") : R.failed("状态修改失败");
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("save")
    public R saveUser(@RequestBody SysUser user) {
        System.err.println(user);
        boolean b = sysUserService.insertObject(user);
        return b ? R.successed("添加成功") : R.failed("添加失败");
    }

    /**
     * 查询详细用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("seleteUser")
    public R seleteUserById(int userId) {
        SysUser user = sysUserService.selectById(userId);
        return R.successed(user);
    }

    /**
     * 批量删除用户
     *
     * @param userIds
     * @return
     */
    @PostMapping("batchDelete")
    public R deleteUsers(@RequestBody Long[] userIds) {
        boolean b = sysUserService.deleteObjests(userIds);
        return b ? R.successed("删除成功") : R.failed("删除失败");
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("delete/{userId}")
    public R deleteUser(@PathVariable Long userId) {
        boolean b = sysUserService.deleteObjest(userId);
        return b ? R.successed("删除成功") : R.failed("删除失败");
    }

    /**
     * 上传头像到minio
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("uploadAvatar")
    public R uploadAvatar(MultipartFile file) throws Exception {
        String fielName = minimalService.upload(file);
        return R.successed(fielName);
    }

    /**
     * 从minio下载头像
     *
     * @param fileName
     * @param response
     * @throws Exception
     */
    @GetMapping("downloadAvatar")
    public void downloadAvatar(String fileName, HttpServletResponse response) throws Exception {
        minimalService.downloadFile(fileName, response);
    }
    @GetMapping("downExcel")
    public R downExcel() {
        List<SysUser> sysUsers = sysUserService.selectObjects();
        ExcelUtils.exportExcel(sysUsers, "全部用户数据", "sheet1", response);
        return R.successed("下载成功");
    }
}


